package database

import (
	"log"

	"github.com/spf13/viper"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var MDB *gorm.DB
var err error

// init()方法在模块被引入时自动执行
func MySQLInit() {
	log.Println("database, init")
	// dsn := "root:Chenhao$123456@tcp(linux.server:3306)/gin_essential?charset=utf8mb4&parseTime=True&loc=Local"
	dsn := viper.GetString("mysql.dsn")
	MDB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("failed to connect database, error: " + err.Error())
	}
	log.Println("DB connected")
}
